package Function.ItemUse;

import FCRendering.FCMapAnimationConstant;
import FCRendering.FCRConstants;
import FCRendering.FCRMaster;
import VWorldGameStudio.*;
import VWorldGameStudio.events.MapHiddenEvent;
import io.reactivex.schedulers.Schedulers;

import java.util.ArrayList;
import java.util.Random;

import static FCRendering.FCRMaster.*;
import static FCRendering.FCRMaster.player1;

public class ToolTanCheYi extends AItemUse{

    private boolean isTanCheing = false;

    @Override
    public ItemUseResult directUse() {
        fcv.closeAllShowWindw();
        if(fcv.isBattle()){
            fcv.getMessage().add("不能在战斗中使用");
            fcv.showMessage();
            return new ItemUseResult().useless();
        }
        if(!isTanCheing) {
            isTanCheing = true;
            Schedulers.computation().scheduleDirect(tanCheAniMovingA);
        }
        ItemUseResult useResult = new ItemUseResult();
        return useResult;
    }

    @Override
    public ItemUseResult useInMapTarget(int x, int y) {
        return directUse();
    }

    @Override
    public ItemUseResult useForMonsterTarget(FCBattleOrder battleOrder, FCMonster m) {
        return directUse();
    }

    @Override
    public ItemUseResult useForPlayerTarget(FCPlayerInfo p) {
        return directUse();
    }

    @Override
    public ItemUseResult useForTankTarget(FCTank t) {
        return directUse();
    }

    private Runnable tanCheAniMovingA = new Runnable() {
        private String[] pathOffsetXYA = new String[]{"-1,0","-1,1","0,1","1,1","1,0","1,-1","0,-1","-1,-1","-1,0"};
        private String[] pathOffsetXYB = new String[]{"-1,0","-1,1","0,1","1,1","1,0","1,-1","0,-1","-1,-1"
                ,"-2,0","-2,1","-2,2","-1,2","0,2","1,2","2,2","2,1","2,0","2,-1","2,-2","1,-2","0,-2","-1,-2","-2,-2","-2,-1","-2,0"};
        private String[] pathOffsetXYC = new String[]{"-1,0","-1,1","0,1","1,1","1,0","1,-1","0,-1","-1,-1"
                ,"-2,0","-2,1","-2,2","-1,2","0,2","1,2","2,2","2,1","2,0","2,-1","2,-2","1,-2","0,-2","-1,-2","-2,-2","-2,-1","-2,0"
        ,"-3,0","-3,1","-3,2","-3,3","-2,3","-1,3","0,3","1,3","2,3","3,3","3,2","3,1","3,0","3,-1","3,-2","3,-3","2,-3","1,-3",
        "0,-3","-1,-3","-2,-3","-3,-3","-3,-2","-3,-1","-3,0"};
        @Override
        public void run() {
            int ox = player1.getPlayer1x();
            int oy = player1.getPlayer1y();
            FCMap map = mapLib.getMapOfName(player1.getMapname());
            FCMapAnimation tanCeAni = mapAnimationLibrary.getMapAnimationByName(FCMapAnimationConstant.TancheYIMapAni);
            if(tanCeAni == null){
                return;
            }
            tanCeAni.setVisiable(1);
            tanCeAni.resetAnimation();
            String[] pathOffsetXY = pathOffsetXYA;
            int times = 3;
            if(useItem.getName().equals("tanCheYiB")){
                pathOffsetXY = pathOffsetXYB;
            }else if(useItem.getName().equals("tanCheYiC")){
                pathOffsetXY = pathOffsetXYC;
                times = 2;
            }
            ArrayList<MapHiddenEvent> hiddenEvents = new ArrayList<>(0);
            ArrayList<String> hiddenEventXY = new ArrayList<>(0);
            //扫描三次,找到就退出
            for(int i = 0; i < times; i++){
                for(int j =0;j<pathOffsetXY.length; j++){
                    if(map.getName().equals(player1.getMapname())){
                        String[] strings = pathOffsetXY[j].split(",");
                        String xyKey = (ox+Integer.parseInt(strings[0]))+"*"+(oy+Integer.parseInt(strings[1]));
                        map.dynamicAddMapAnimation(FCMapAnimationConstant.TancheYIMapAni,xyKey);
                        tanCeAni.setVisiable(1);

                        String keyName = xyKey+"*"+map.getName();
                        if(!hiddenEventXY.contains(keyName) && !gameSave.getHiddenItemWasTakeList().contains(keyName)){
                           FCEvent hidden = FCRMaster.eventLib.getEventOfName(map.getEventNameOfCoor((ox+Integer.parseInt(strings[0])), (oy+Integer.parseInt(strings[1]))));
                            if(hidden != null && hidden.getType().equals("mapHiddenItem")){
                                hiddenEvents.add((MapHiddenEvent) hidden);
                                hiddenEventXY.add(keyName);
                            }
                        }

                        try {
                            Thread.sleep(80);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }else {
                        map.removeAlldynamicMapAnimation();
                        isTanCheing = false;
                        tanCeAni.setVisiable(0);
                        return;
                    }
                }
                try {
                    tanCeAni.setVisiable(0);
                    Thread.sleep(300);
                }catch (Exception e){}
            }
            map.removeAlldynamicMapAnimation();
            isTanCheing = false;
            tanCeAni.setVisiable(0);

            String randItem = null;
            int randMoney = 0;
            Random random = new Random();
            if(hiddenEvents.size() == 0){
                if(random.nextFloat() > 0.8){
                    //扫到了随机奖励
                    if(random.nextFloat()>0.75){
                        float t = random.nextFloat();
                        if(t < 0.8){
                            randItem = "tongBang";
                        }else if(t < 0.95){
                            randItem = "yinBang";
                        }else {
                            randItem = "jinBang";
                        }
                    }else {
                        randMoney = random.nextInt(120);
                        fcv.getMessage().add("探测仪:发现金币"+randMoney+"G!");
                        fcv.showMessage();
                        player1.setMoney(player1.getMoney()+randMoney);
                        return;
                    }
                }else {
                    fcv.getMessage().add("探测仪:什么也没探测到!");
                    fcv.showMessage();
                    return;
                }
            }else {
                int getMoney = 0;
                ArrayList<FCEvent> r = new ArrayList<>();
                for(MapHiddenEvent event: hiddenEvents){
                    if(event.getMoney() > 0){
                        getMoney += event.getMoney();
                        r.add(event);
                    }
                }
                if(getMoney > 0) {
                    fcv.getMessage().add("探测仪:发现金币" + getMoney + "G!");
                    fcv.showMessage();
                    player1.setMoney(player1.getMoney() + getMoney);
                    for(FCEvent event: r){
                        int index = hiddenEvents.indexOf(event);
                        gameSave.getHiddenItemWasTakeList().add(hiddenEventXY.get(index));
                        hiddenEventXY.remove(index);
                        hiddenEvents.remove(index);
                    }
                }
                if(hiddenEvents.size() == 0){
                    return;
                }
            }
            //TODO 展示探测的结果
            fcv.closeAllShowWindw();
            fcv.getTancheResult().reBuildWindow(randItem, hiddenEvents, hiddenEventXY);
            fcv.setOpenTanCheResult(true);
        }
    };
}
